﻿using System.Xml.Linq;
using Engine;
using Test1;

namespace Game
{
    public class MCCGSellWidget : CanvasWidget
    {
        public ComponentMiner m_componentMiner;

        public ComponentTest1 m_componentTest1;

        public GridPanelWidget m_inventoryGrid;

        public InventorySlotWidget m_sellSlot;

        public BevelledButtonWidget m_changeButton;

        public BevelledButtonWidget m_sellButton;

        public LabelWidget m_price;

        public MCCGSellWidget(ComponentMiner componentMiner)
        {
            m_componentMiner = componentMiner;
            m_componentTest1 = m_componentMiner.Entity.FindComponent<ComponentTest1>();
            XElement node = ContentManager.Get<XElement>("Widgets/Systemshop/MCCGSellWidget");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
            m_sellSlot = Children.Find<InventorySlotWidget>("SellSlot");
            m_changeButton = Children.Find<BevelledButtonWidget>("ChangeButton");
            m_sellButton = Children.Find<BevelledButtonWidget>("SellButton");
            m_price = Children.Find<LabelWidget>("Price");
            int num = 10;
            for (int i = 0; i < m_inventoryGrid.RowsCount; i++)
            {
                for (int j = 0; j < m_inventoryGrid.ColumnsCount; j++)
                {
                    InventorySlotWidget inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(componentMiner.Inventory, num++);
                    m_inventoryGrid.Children.Add(inventorySlotWidget);
                    m_inventoryGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }

            m_sellSlot.AssignInventorySlot(m_componentTest1, 0);
        }

        public override void Update()
        {
            if (!m_componentMiner.IsAddedToProject || m_componentMiner.ComponentCreature.ComponentHealth.Health <= 0f)
            {
                base.ParentWidget.Children.Remove(this);
            }

            if (m_changeButton.IsClicked)
            {
                m_componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new MCCGPurchaseWidget(m_componentMiner);
            }

            int slotValue = m_componentTest1.GetSlotValue(0);
            int num = Terrain.ExtractContents(slotValue);
            int num2 = Terrain.ExtractData(slotValue);
            int num3 = 0;
            int slotCount = m_componentTest1.GetSlotCount(0);
            if (slotCount > 0)
            {
                num3 = MoneyManager.GetReturnMoney(slotValue) * slotCount;
            }

            if (num3 > 0)
            {
                m_price.Text = $"+{num3}{MoneyManager.CoinChar}";
                m_sellButton.Color = Color.White;
                if (m_sellButton.IsClicked)
                {
                    m_componentTest1.RemoveSlotItems(0, slotCount);
                    MoneyManager.AddItem(m_componentMiner, m_componentMiner.Project.FindSubsystem<SubsystemPickables>(), MoneyManager.CoinValue, num3);
                    m_componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage($"已出售{slotCount}个{MoneyManager.GetBlockDisplayName(slotValue)}，获得{num3}{MoneyManager.CoinChar}", Color.White, blinking: true, playNotificationSound: false);
                }
            }
            else
            {
                m_price.Text = string.Empty;
                m_sellButton.Color = Color.Gray;
                if (slotCount > 0 && m_sellButton.IsClicked)
                {
                    m_componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("这个物品无法出售", Color.White, blinking: true, playNotificationSound: false);
                }
            }
        }
    }
}